package com.lss.controller;

import com.lss.bean.Score;
import com.lss.bean.Student;
import com.lss.service.ScoreService;
import com.lss.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理学生成绩的数据传送和页面跳转
 */
@Controller
public class ScoreController {

    @Autowired
    ScoreService scoreService;

    @Autowired
    StudentService service;

    /**
     * 展示成绩页面
     *
     * @param start
     * @param rows
     * @param model
     * @return
     */
    @RequestMapping("/scoreLimit")
    public String scoreLimit(int start, int rows, Model model) {
        List<Score> scores = scoreService.selectScoreByLimit(start, rows);
        model.addAttribute("scores", scores);
        List<Score> allScore = scoreService.selectAllScore();
        model.addAttribute("allScore", allScore);
        model.addAttribute("start", start);
        return "scoreList";
    }

    /**
     * 删除成绩
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteScore")
    public String deleteScore(String id) {
        scoreService.deleteScore(id);
        return "redirect:/scoreLimit?start=0&rows=6";
    }

    /**
     * 修改成绩页面跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/updatePageJump")
    public String updatePageJump(String id, Model model) {
        Score score = scoreService.selectScoreById(id);
        model.addAttribute("score", score);
        return "updateScore";
    }

    //跳转到添加学生成绩页面
    @RequestMapping("/addScorePageJump")
    public String addScore() {
        return "addScore";
    }

    /**
     * 添加学生成绩
     *
     * @param score
     * @return
     */
    @RequestMapping("/addScore")
    public String addScore(Score score) {
        Score selectScoreById = scoreService.selectScoreById(score.getId());
        if (selectScoreById != null) {    //如果学号重复，返回页面
            return "redirect:/addScorePageJump";
        }
        scoreService.addScore(score);
        return "redirect:/scoreLimit?start=0&rows=6";
    }

    @RequestMapping("/updateScorePageJump")
    public String updateScore(@RequestParam("id") String id, Model model) {
        Score score = scoreService.selectScoreById(id);
        model.addAttribute("score", score);
        return "updateScore";
    }

    /**
     * 修改学生信息
     */
    @RequestMapping("/updateScore")
    public String updateScore(Score score) {
        scoreService.updateScore(score);
        return "redirect:/scoreLimit?start=0&rows=6";
    }

    /**
     * 验证此学号是否存在
     */
    @ResponseBody
    @RequestMapping("/checkingStudentId")
    public int checkingStudentId(String id) {
        Score score = scoreService.selectScoreById(id);
        if (score != null) {
            return 0;
        }
        Student student = service.selectById(id);
        if (student == null) {
            return 1;
        } else {
            return 2;
        }
    }

    @RequestMapping("/findScoreNews")
    public String findScoreNews(String scoreId, String scoreName, Model model) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!scoreId.equals("")) {
            map.put("id", scoreId);
        }
        if (!scoreName.equals("")) {
            map.put("name", scoreName);
        }
        map.put("start", 0);
        map.put("rows", 6);
        List<Score> allScores = scoreService.selectScoreNews(map);
        model.addAttribute("allScore", allScores);
        List<Score> scores = scoreService.selectScoreNewsByLimit(map);
        model.addAttribute("scores", scores);
        model.addAttribute("start", 0);
        return "scoreList";
    }
}
